package com.zj.blessings.adapter;

import java.util.ArrayList;
import java.util.List;
import com.zj.blessings.R;
import com.zj.blessings.form.RemoteCategoryItem;
import com.zj.blessings.util.ResourceUtils;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class RemoteCategoryAdapter extends BaseAdapter {
	private List<RemoteCategoryItem> mCategoryItems = new ArrayList<RemoteCategoryItem>();
	private Context mContext;
	private int[] mRes;

	public RemoteCategoryAdapter(Context context) {
		this.mContext = context;
		mRes = ResourceUtils.getRes();
	}

	public void setRemoteCategoryItems(
			List<RemoteCategoryItem> remoteCategoryItems) {
		mCategoryItems.clear();
		if (remoteCategoryItems != null) {
			for (RemoteCategoryItem categoryItem : remoteCategoryItems) {
				mCategoryItems.add(categoryItem);
			}
		}
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return mCategoryItems.size();
	}

	@Override
	public Object getItem(int position) {
		return mCategoryItems.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder vh = null;
		if (convertView == null) {
			vh = new ViewHolder();
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.remote_category_item, null);
			vh.image = (ImageView) convertView
					.findViewById(R.id.remoteCategoryItemImage);
			vh.category = (TextView) convertView
					.findViewById(R.id.remoteCategoryItemCategory);
			vh.count = (TextView) convertView
					.findViewById(R.id.remoteCategoryItemCategoryCount);
			convertView.setTag(vh);
		} else {
			vh = (ViewHolder) convertView.getTag();
		}
		if (mRes != null && mRes.length > 0) {
			vh.image.setBackgroundResource(mRes[position % mRes.length]);
		}
		vh.category.setText(mCategoryItems.get(position).getCategory());
		vh.count.setText(String
				.valueOf(mCategoryItems.get(position).getCount()));
		return convertView;
	}

	class ViewHolder {
		ImageView image;
		TextView category;
		TextView count;
	}
}
